{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "31b98928-2160-4f15-8d3f-9afb7df542e7",
   "metadata": {},
   "source": [
    "### 1. 创建和初始化字符串\n",
    "\n",
    "#### 直接赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "696ff5d5-6588-4309-8fd3-a431601e4271",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, World!\n"
     ]
    }
   ],
   "source": [
    "s = \"Hello, World!\"\n",
    "# 直接使用引号（单引号或双引号均可）来创建字符串\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17b7c826-cd2f-4e24-a643-138af8436d2c",
   "metadata": {},
   "source": [
    "使用构造函数 str()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "061faa09-68bf-4ffe-acb1-5dd008f79397",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "s = str(123)\n",
    "# 使用str()函数将数字转换为字符串\n",
    "print(s)\n",
    "print(type(s))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1477f36-4db2-4914-ba01-4037225a6def",
   "metadata": {},
   "source": [
    "### 2. 访问字符串中的元素\n",
    "\n",
    "#### 通过索引访问"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8072bdf6-9b3a-41e5-9892-17092431ffc1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H\n"
     ]
    }
   ],
   "source": [
    "s = \"Hello, World!\"\n",
    "print(s[0])  # H\n",
    "# 使用索引来访问字符串中的第一个字符（索引从0开始）\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dfe3f2e0-d7e5-41be-b5fb-91c1121f2997",
   "metadata": {},
   "source": [
    "使用切片操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3f0de29e-3690-420e-80b3-dbda76f0c0da",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ello\n"
     ]
    }
   ],
   "source": [
    "s = \"Hello, World!\"\n",
    "print(s[1:5])  # ello\n",
    "# 使用切片来访问字符串中的一部分，这里获取从第2个（索引1）到第5个（索引4）的字符\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "668833ff-eafe-4590-944b-d9814338a7ee",
   "metadata": {},
   "source": [
    "### 3. 字符串的基本操作\n",
    "\n",
    "#### 连接 `+`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4222728b-0c3f-459b-8677-527429e1a28a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, Alice!\n"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "787f73b0-9860-40b0-8905-15b6fb535de5",
   "metadata": {},
   "source": [
    "#### 连接 `*`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "eaa96941-464a-4bc9-a434-b4b2e24d7a79",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "echo echo echo \n"
     ]
    }
   ],
   "source": [
    "echo = \"echo \"\n",
    "result = echo * 3\n",
    "print(result)  # echo echo echo \n",
    "# 使用星号（*）重复字符串\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6bb6687d-95d4-442a-8d1a-e10021e86b8b",
   "metadata": {},
   "source": [
    "检查字符串是否包含某个子串 `in`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "dfb5d37f-8613-4887-a702-fb1d9442a9a0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The word is in the sentence\n"
     ]
    }
   ],
   "source": [
    "sentence = \"The quick brown fox jumps over the lazy dog\"\n",
    "word = \"fox\"\n",
    "if word in sentence:\n",
    "    print(\"The word is in the sentence\")\n",
    "else:\n",
    "    print(\"The word is not in the sentence\")\n",
    "# 使用in操作符检查一个字符串是否包含另一个字符串\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef65459f-1d2f-4b09-82e7-c5957f086ff8",
   "metadata": {},
   "source": [
    "### 4. 字符串的大小写转换\n",
    "\n",
    "#### `lower()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ce37e6b5-d9c8-4b20-a815-86f3fa863eca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello, world!\n"
     ]
    }
   ],
   "source": [
    "s = \"HELLO, WORLD!\"\n",
    "print(s.lower())  # hello, world!\n",
    "# 将字符串中所有大写字符转换为小写\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09c12496-111f-4e1d-a4ac-021eb526ed30",
   "metadata": {},
   "source": [
    "`upper()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "daf26959-864a-48d1-9680-dfe1ed1ab3be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HELLO, WORLD!\n"
     ]
    }
   ],
   "source": [
    "s = \"hello, world!\"\n",
    "print(s.upper())  # HELLO, WORLD!\n",
    "# 将字符串中的所有小写字符转换为大写\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b64a3a39-b58d-4f2d-a3ba-93073f52172c",
   "metadata": {},
   "source": [
    "`capitalize()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "0662a7c3-38dc-4b36-a6dd-db1913125eeb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, world!\n"
     ]
    }
   ],
   "source": [
    "s = \"hello, world!\"\n",
    "print(s.capitalize())  # Hello, world!\n",
    "# 将字符串的第一个字符转换为大写\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60b11aa1-f9e6-4b64-8baf-9f8864e645c7",
   "metadata": {},
   "source": [
    "`title()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "38e76ec9-8cec-4916-9bab-c56359d1bcf7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, World!\n"
     ]
    }
   ],
   "source": [
    "s = \"hello, world!\"\n",
    "print(s.title())  # Hello, World!\n",
    "# 将字符串中每个单词的首字母转换为大写\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05e41c1a-96bf-4fd0-92a1-5049f86357b2",
   "metadata": {},
   "source": [
    "`swapcase()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "d60dbd3c-52c6-4367-a65a-b81f360455b9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hELLO, wORLD!\n"
     ]
    }
   ],
   "source": [
    "s = \"Hello, World!\"\n",
    "print(s.swapcase())  # hELLO, wORLD!\n",
    "# 将字符串中的小写字母转换为大写字母，大写字母转换为小写字母\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "24379200-9138-4888-b546-3bf3f1138d6e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hELLO, wORLD!\n"
     ]
    }
   ],
   "source": [
    "s = \"Hello, World!\"\n",
    "print(s.swapcase())  # hELLO, wORLD!\n",
    "# 将字符串中的小写字母转换为大写字母，大写字母转换为小写字母\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6694ec3-474c-4d01-8770-aa21aa31a142",
   "metadata": {},
   "source": [
    "### 5. 字符串的查找和替换\n",
    "\n",
    "#### `find()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "cb5b5c76-c5fa-41c7-a1a1-9b0c65f3650f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n"
     ]
    }
   ],
   "source": [
    "s = \"hello, world!\"\n",
    "print(s.find(\"world\"))  # 7\n",
    "# 查找子字符串\"world\"第一次出现的位置\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5954017-7399-4504-a9c6-0d1c9fd7b3a1",
   "metadata": {},
   "source": [
    "`rfind()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "911fd4dd-4786-4d2c-8f19-f5d6ffe78498",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n"
     ]
    }
   ],
   "source": [
    "s = \"hello, world, meet Python!\"\n",
    "print(s.rfind(\"world\"))  # 7\n",
    "# 从字符串的右侧开始查找子字符串\"world\"第一次出现的位置\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1272acfb-4cab-4262-a105-e9aafbf03a5d",
   "metadata": {},
   "source": [
    "`index()`, `rindex()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "cf7c3bd8-c48e-4377-b1f9-18d7c95ef436",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n"
     ]
    }
   ],
   "source": [
    "s = \"hello, world!\"\n",
    "print(s.index(\"world\"))  # 7\n",
    "# 与find()类似，但如果子字符串不在字符串中则会抛出异常\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "7cb4650b-386c-4d07-bdbf-26d854a07c3f",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "substring not found",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[25], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m s \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhello, world!\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43ms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mindex\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mwold\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m)\n",
      "\u001b[1;31mValueError\u001b[0m: substring not found"
     ]
    }
   ],
   "source": [
    "s = \"hello, world!\"\n",
    "print(s.index(\"wold\"))  # 7\n",
    "# 与find()类似，但如果子字符串不在字符串中则会抛出异常\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "4fa20b88-dc14-4df7-bdb2-f111bee7633a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-1\n"
     ]
    }
   ],
   "source": [
    "s = \"hello, world!\"\n",
    "print(s.find(\"wold\"))  # 7\n",
    "# 查找子字符串\"world\"第一次出现的位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "04edf97b-e52f-4e1e-84e0-84260177383e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n"
     ]
    }
   ],
   "source": [
    "s = \"hello, world!\"\n",
    "print(s.rindex(\"world\"))  # 7\n",
    "# 与find()类似，但如果子字符串不在字符串中则会抛出异常"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f3ac932-c1c6-466e-ac35-e2775cf7f41d",
   "metadata": {},
   "source": [
    "`replace()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "e3dcb9d4-880f-4b54-91d9-f0d90c88aa13",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello, Python!\n"
     ]
    }
   ],
   "source": [
    "s = \"hello, world!\"\n",
    "print(s.replace(\"world\", \"Python\"))  # hello, Python!\n",
    "# 替换字符串中的子串\"world\"为\"Python\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3607b01-fbc5-45c3-bdb8-91c4312c6e67",
   "metadata": {},
   "source": [
    "### 6. 字符串的分割和合并\n",
    "\n",
    "#### `split()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ad8483a9-e667-4222-b773-f48bd8c84c7a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['hello', 'world', 'meet Python']\n"
     ]
    }
   ],
   "source": [
    "s = \"hello, world, meet Python\"\n",
    "print(s.split(\", \"))  # ['hello', 'world', 'meet Python']\n",
    "# 按照指定的分隔符\",\"将字符串分割成列表\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "3c8c0b8f-9c4f-44de-937a-1aaac5413434",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['hello', 'world', 'meet Python']\n"
     ]
    }
   ],
   "source": [
    "s = \"hello, world, meet Python\"\n",
    "print(s.split(\", \",2))  # ['hello', 'world', 'meet Python']\n",
    "# 按照指定的分隔符\",\"将字符串分割成列表\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e717e8ee-0308-461d-9129-dd703749c986",
   "metadata": {},
   "source": [
    "`rsplit()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "efff923f-74fd-4897-8847-5c6ad6f51af3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['hello, world', 'meet Python']\n"
     ]
    }
   ],
   "source": [
    "s = \"hello, world, meet Python\"\n",
    "print(s.rsplit(\", \", 1))  # ['hello, world', 'meet Python']\n",
    "# 从字符串的右侧开始分割，这里最多分割1次\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7059216-f7fe-47ba-8787-9f488f9efa87",
   "metadata": {},
   "source": [
    "`splitlines()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "72e89cd5-f34f-4e65-aedf-b9780d3ac9c6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['hello', 'world', 'meet', 'Python']\n"
     ]
    }
   ],
   "source": [
    "s = \"hello\\nworld\\nmeet\\nPython\"\n",
    "print(s.splitlines())  # ['hello', 'world', 'meet', 'Python']\n",
    "# 按照行('\\n', '\\r', '\\r\\n')分割字符串\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f18027c-e381-4ebf-9b99-d05334581119",
   "metadata": {},
   "source": [
    "`join()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "63a35702-254d-4cb9-92f9-93568d80c773",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello, world, meet, Python\n"
     ]
    }
   ],
   "source": [
    "words = [\"hello\", \"world\", \"meet\", \"Python\"]\n",
    "print(\", \".join(words))  # hello, world, meet, Python\n",
    "# 将序列中的元素以指定的字符\", \"连接生成一个新的字符串\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5cac89ae-3dc2-42a1-a838-119a1a2be69c",
   "metadata": {},
   "source": [
    "### 7. 字符串的去除空白\n",
    "\n",
    "#### `strip()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "ff6b4fef-3da3-4796-84e3-cfe09108a30e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello, world!\n"
     ]
    }
   ],
   "source": [
    "s = \"  hello, world!  \"\n",
    "print(s.strip())  # \"hello, world!\"\n",
    "# 去除字符串两端的空白字符\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2e85321-93df-478e-8e34-46bbf1564a18",
   "metadata": {},
   "source": [
    "`lstrip()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "68b44f35-14b1-4323-b50a-c669147f6eac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello, world!  \n"
     ]
    }
   ],
   "source": [
    "s = \"  hello, world!  \"\n",
    "print(s.lstrip())  # \"hello, world!  \"\n",
    "# 去除字符串左侧的空白字符\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c25c837-9264-443c-9e11-3a206d79a286",
   "metadata": {},
   "source": [
    "`rstrip()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "e40367dd-c78e-4752-97df-8668707df805",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  hello, world!\n"
     ]
    }
   ],
   "source": [
    "s = \"  hello, world!  \"\n",
    "print(s.rstrip())  # \"  hello, world!\"\n",
    "# 去除字符串右侧的空白字符\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65691e3c-8904-4ad7-bbfc-c0f8dd90f5d6",
   "metadata": {},
   "source": [
    "### 8. 字符串的格式化\n",
    "\n",
    "#### 使用 `%` 操作符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "dccd8478-3930-4185-a258-70dd812726c7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name: Alice, Age: 30\n"
     ]
    }
   ],
   "source": [
    "name = \"Alice\"\n",
    "age = 30\n",
    "print(\"Name: %s, Age: %d\" % (name, age))  # Name: Alice, Age: 30\n",
    "# 使用 % 操作符进行字符串格式化\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a633fa53-8ec1-41d2-bcb2-225a4846ab85",
   "metadata": {},
   "source": [
    "1. `%d` - 整数\n",
    "2. `%f` - 浮点数\n",
    "3. `%s` - 字符串\n",
    "4. `%x` - 十六进制\n",
    "5. `%o` - 八进制\n",
    "6. `%e` - 科学计数法\n",
    "7. `%10d` - 右对齐，总宽度为10的整数\n",
    "8. `%-10d` - 左对齐，总宽度为10的整数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1314a856-8c82-4124-9f2f-b16043f1b737",
   "metadata": {},
   "source": [
    "使用 `format()` 方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "5952a7fd-c685-4f4b-a88b-78f74ce904d2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name: Alice, Age: 30\n"
     ]
    }
   ],
   "source": [
    "name = \"Alice\"\n",
    "age = 30\n",
    "print(\"Name: {}, Age: {}\".format(name, age))  # Name: Alice, Age: 30\n",
    "# 使用format()方法进行字符串格式化\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81426e4f-3741-42fc-9dcc-a551331a0659",
   "metadata": {},
   "source": [
    "使用 f-strings (Python 3.6+)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "fd5d20f0-20ee-4494-a423-c83f8b61295d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name: Alice, Age: 30\n"
     ]
    }
   ],
   "source": [
    "name = \"Alice\"\n",
    "age = 30\n",
    "print(f\"Name: {name}, Age: {age}\")  # Name: Alice, Age: 30\n",
    "# 使用f-strings进行字符串格式化，更简洁明了\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6b41934-af17-4644-bc6c-62f5bb56fd8b",
   "metadata": {},
   "source": [
    "f-strings 可以替代 format() 和 % 进行字符串格式化。f-strings 是 Python 3.6 引入的一种字符串格式化方法，它使用大括号 {} 和表达式来表示要插入的变量值。与 % 和 format() 相比，f-strings 语法更加简洁和直观f-strings 可以替代 format() 和 % 进行字符串格式化。f-strings 是 Python 3.6 引入的一种字符串格式化方法，它使用大括号 {} 和表达式来表示要插入的变量值。与 % 和 format() 相比，f-strings 语法更加简洁和直观"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8487584b-f4b6-466b-9931-f3a2ce6d59fc",
   "metadata": {},
   "source": [
    "使用 f-strings 的优点包括：\n",
    "\n",
    "更简洁：不需要像 % 格式化字符串一样使用特殊符号，并且不需要像 format() 方法一样调用额外的方法。\n",
    "更直观：在字符串中直接嵌入变量名，使得代码更易读易懂。\n",
    "更高效：性能比 % 和 format() 方法更好，尤其是在大量字符串格式化时。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6bf8faf5-b364-4b92-9d64-dd0a1aae2c19",
   "metadata": {},
   "source": [
    "### 9. 字符串的编码与解码\n",
    "\n",
    "#### `encode()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "44323ab2-0bd0-4644-be7c-0a937dd9eb7e",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = \"hello, world!\"\n",
    "encoded_s = s.encode(\"utf-8\")\n",
    "# 将字符串编码为指定的编码格式，这里是UTF-8\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d02ada55-7484-4add-9ba6-0609aaef90c8",
   "metadata": {},
   "source": [
    "### 10. 其他有用的字符串方法\n",
    "\n",
    "#### `count()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "bcb190b1-66de-4540-b2f0-9a7956ce09ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "s = \"hello, world! world!\"\n",
    "print(s.count(\"world\"))  # 2\n",
    "# 计算子字符串在字符串中出现的次数\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61d98e45-354c-47c5-92bf-3ad5084f91c9",
   "metadata": {},
   "source": [
    "`startswith()`、`endswith()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "ae767cd7-feb7-4e49-be95-f8b3e4cf8169",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "s = \"hello, world!\"\n",
    "print(s.startswith(\"hello\"))  # True\n",
    "print(s.endswith(\"world!\"))  # True\n",
    "# 检查字符串是否以指定的前缀开始或以指定的后缀结束\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c22d5c71-f9ab-433d-8ead-8d047d3ae690",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(\"abc123\".isalpha())  # False\n",
    "# 检查字符串是否全部由字母组成。这里字符串\"abc123\"包含数字，所以返回False。\n",
    "\n",
    "print(\"123\".isdigit())  # True\n",
    "# 检查字符串是否全部由数字组成。字符串\"123\"完全由数字构成，所以返回True。\n",
    "\n",
    "print(\"abc\".islower())  # True\n",
    "# 检查字符串中的字母是否都是小写。字符串\"abc\"全部由小写字母组成，所以返回True。\n",
    "\n",
    "print(\"ABC\".isupper())  # True\n",
    "# 检查字符串中的字母是否都是大写。字符串\"ABC\"全部由大写字母组成，所以返回True。\n",
    "\n",
    "print(\" \".isspace())  # True\n",
    "# 检查字符串是否只包含空白字符。这里的字符串只包含一个空格字符，所以返回True。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9fa5624b-55cf-4d1e-a194-e90ce39c1e9b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
